#include "../src/thread/thread.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//ABDH#K###E##CFI###G#J##

int main(int argc, const char *argv[])
{
    int ret = 0;
    BiTNode *tree = NULL;

    // 创建一个二叉树
    printf("请按照先序输入二叉树(空节点用#表示)：");
    tree = fBiTree.create();

    if (tree != NULL)
    {
        printf("二叉树创建成功！\n");
    }
    else
    {
        printf("二叉树创建出现异常！\n");
        ret = -1;
        goto ERROR_END;
    }

    printf("遍历结果列表为：\n");
    printf("     先序遍历输出     |      中序遍历输出     |      后序遍历输出     |    层序遍历输出\n");
    fBiTree.prevOrder(tree);
    printf("| ");
    fBiTree.inOrder(tree);
    printf("| ");
    fBiTree.postOrder(tree);
    printf("| ");
    fBiTree.levelOrder(tree);
    printf("\n\n");

    int leaf = 0;
    fBiTree.leafNum(tree, &leaf);
    printf("节点的个数 = %d  叶子节点的个数 = %d, 树的深度/高度 = %d\n", fBiTree.nodeNum(tree), leaf, fBiTree.depth(tree));

    BiTNode *copyTree1 = fBiTree.copy(tree);
    BiTNode *copyTree2 = fBiTree.copy(tree);

    /**
     *  线索化二叉树
     *  线索化二叉树会修改二叉树的结构，修改后将不再是原本的二叉树的结构了
     **/
    BiTNode *p = NULL;
    fthread.bylchild(copyTree1, &p);

    printf("copy树的左孩子前序线索化遍历输出：");
    fthread.lchild_print(copyTree1);
    printf("\n");

    /* 创建一个顺序表 */
    seqList *seqlist = fSeqList.create(fBiTree.nodeNum(tree));
    printf("copy树的顺序表前序线索化遍历输出：");
    fthread.bySeqlist(copyTree2, seqlist);
    fthread.seqlist_print(seqlist);

    printf("copy树的顺序表前序线索化后遍历结果列表为：\n");
    printf("     先序遍历输出     |      中序遍历输出     |      后序遍历输出     |    层序遍历输出\n");
    fBiTree.prevOrder(copyTree2);
    printf("| ");
    fBiTree.inOrder(copyTree2);
    printf("| ");
    fBiTree.postOrder(copyTree2);
    printf("| ");
    fBiTree.levelOrder(copyTree2);
    printf("\n\n");

    /* 创建一个链式表 */
    linkList *linklist = flinklist.create();
    printf("copy树的链式表前序线索化遍历输出：");
    fthread.bylinkList(copyTree2, linklist);
    fthread.linklist_print(linklist);
    printf("\n");

    printf("copy树的链式表前序线索化后遍历结果列表为：\n");
    printf("     先序遍历输出     |      中序遍历输出     |      后序遍历输出     |    层序遍历输出\n");
    fBiTree.prevOrder(copyTree2);
    printf("| ");
    fBiTree.inOrder(copyTree2);
    printf("| ");
    fBiTree.postOrder(copyTree2);
    printf("| ");
    fBiTree.levelOrder(copyTree2);
    printf("\n\n");

ERROR_END:
    // 释放二叉树
    fBiTree.release(tree);
    /* copyTree1已经在线索化的过程中别修改，此时按照原先的释放方式释放的话出现异常 */
    // fBiTree.release(copyTree1);
    fBiTree.release(copyTree2);

    // 销毁顺序表
    fSeqList.destory(seqlist);
    // 销毁链表
    flinklist.destory(&linklist);

    printf("system exit with return code %d\n", ret);

    return ret;
}
// ABDH#K###E##CFI###G#J##
